<?php
class Admin_GamesController extends Zend_Controller_Action 

{
	
	// Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	protected $_page = 1;
	protected $_name = '';
	protected $_cat = 0;
	
	// Duong dan cua Action chinh
	protected $_actionMain;
	public function init() 

	{
		
		// Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams ();
		
		$this->_page = $this->_arrParam ['page'];
		
		$this->_name = $this->_arrParam ['name'];
		
		$this->_cat = $this->_arrParam ['parent'];
		
		if ($this->_page == '' || $this->_page <= 0) {
			
			$this->_page = 1;
		}
		
		if ($this->_cat == '' || $this->_cat <= 0) {
			
			$this->_cat = 0;
		}
		
		// Duong dan cua Action chinh
		
		if ($this->_cat) {
			
			$this->_actionMain = '/' . $this->_arrParam ['module'] . 

			'/' . $this->_arrParam ['controller'] . '/index/page/' . $this->_page . '/cat/' . $this->_cat;
		} else {
			
			$this->_actionMain = '/' . $this->_arrParam ['module'] . 

			'/' . $this->_arrParam ['controller'] . '/index/page/' . $this->_page;
		}
		
		if ($this->_name) {
			
			$this->_actionMain .= '/name/' . $this->_name;
		}
	}
	public function indexAction() 

	{
		$translate = Zend_Registry::get ( 'Zend_Translate' );
		
		$this->view->title = $translate->_ ( "TITLE_ADMIN_GAMES_INDEX" );
		
		$this->view->headTitle ( $this->view->title );
		
		$menuModel = new Admin_Model_Menu ();
		
		$listMenu = $menuModel->fetchMenu ( array (
				'parent' => '' 
		) );
		
		$listMenu = $listMenu->toArray ();
		
		$optionsMenu = array (
				'0' => 'Tất Cả' 
		);
		
		foreach ( $listMenu as $key => $value ) {
			
			$optionsMenu [$value ['id']] = $value ['name'];
			
			$listSub = $menuModel->fetchMenu ( array (
					'parent' => $value ['id'] 
			) );
			
			$listSub = $listSub->toArray ();
			
			foreach ( $listSub as $keySub => $valueSub ) {
				
				$optionsMenu [$valueSub ['id']] = '--' . $valueSub ['name'];
			}
		}
		
		$this->view->filter = $this->_name;
		
		$this->view->cat = $this->_cat;
		
		$gamesUser = new Admin_Model_Games ();
		
		if ($this->_cat > 0) {
			
			$listGames = $gamesUser->searchGames ( $this->_cat, $this->_name );
		} else {
			
			if ($this->_name) {
				
				$listGames = $gamesUser->fetchGames ( array (
						'name' => $this->_name 
				) );
			} else {
				
				$listGames = $gamesUser->fetchGames ();
			}
		}
		
		$paginator = Zend_Paginator::factory ( $listGames );
		
		$paginator->setItemCountPerPage ( NUM_PER_PAGE );
		
		$paginator->setCurrentPageNumber ( $this->_page );
		
		$this->view->page = $this->_page;
		
		$this->view->paginator = $paginator;
		
		$this->view->select_cat = $optionsMenu;
	}
	public function flagAction() 

	{
		$translate = Zend_Registry::get ( 'Zend_Translate' );
		
		$this->view->title = $translate->_ ( "TITLE_ADMIN_GAMES_INDEX" );
		
		$this->view->headTitle ( $this->view->title );
		
		$menuModel = new Admin_Model_Menu ();
		
		$listMenu = $menuModel->fetchMenu ( array (
				'parent' => '' 
		) );
		
		$listMenu = $listMenu->toArray ();
		
		$optionsMenu = array (
				'0' => 'Tất Cả' 
		);
		
		foreach ( $listMenu as $key => $value ) {
			
			$optionsMenu [$value ['id']] = $value ['name'];
			
			$listSub = $menuModel->fetchMenu ( array (
					'parent' => $value ['id'] 
			) );
			
			$listSub = $listSub->toArray ();
			
			foreach ( $listSub as $keySub => $valueSub ) {
				
				$optionsMenu [$valueSub ['id']] = '--' . $valueSub ['name'];
			}
		}
		
		$this->view->filter = $this->_name;
		
		$this->view->cat = $this->_cat;
		
		$gamesUser = new Admin_Model_Games ();
		
		if ($this->_cat > 0) {
			
			$listGames = $gamesUser->searchGames ( $this->_cat, $this->_name );
		} else {
			
			if ($this->_name) {
				
				$listGames = $gamesUser->fetchGames ( array (
						'name' => $this->_name 
				) );
			} else {
				
				$listGames = $gamesUser->fetchGames ( array (
						'flag' => 1 
				) );
			}
		}
		
		$paginator = Zend_Paginator::factory ( $listGames );
		
		$paginator->setItemCountPerPage ( NUM_PER_PAGE );
		
		$paginator->setCurrentPageNumber ( $this->_page );
		
		$this->view->page = $this->_page;
		
		$this->view->paginator = $paginator;
		
		$this->view->select_cat = $optionsMenu;
	}
	public function insertAction() 

	{
		$translate = Zend_Registry::get ( 'Zend_Translate' );
		
		$this->view->title = $translate->_ ( "TITLE_ADMIN_GAMES_INSERT" );
		
		$this->view->headTitle ( $this->view->title );
		
		$menuModel = new Admin_Model_Menu ();
		
		$this->view->slbCategory = $menuModel->itemInSelectbox ();
		
		$modelServer = new Admin_Model_Server ();
		
		$listServer = $modelServer->fetchServer ( array (
				'status' => 1 
		) );
		
		$listServer = $listServer->toArray ();
		
		$url_options = array ();
		
		foreach ( $listServer as $k => $v ) {
			
			$url_options [$v ['id']] = $v ['name'] . ' - ' . $v ['url_server'];
		}
		
		$this->view->url_options = $url_options;
		
		$type_game_option = array (
				'Game Flash',
				'Video Youtube',
				'Iframe' 
		);
		
		$this->view->type_game_option = $type_game_option;
		
		if ($this->_request->isPost ()) {
			
			$name = $this->_arrParam ['name'];
			
			$keywords = $this->_arrParam ['keywords'];
			
			$desc = $this->_arrParam ['desc'];
			
			$content = $this->_arrParam ['content'];
			
			$instructions = $this->_arrParam ['instructions'];
			
			$cat_game = $this->_arrParam ['cat_game'];
			
			$source_image = $this->_arrParam ['source_image'];
			
			$source_fileurl = $this->_arrParam ['source_fileurl'];
			
			$source_url = $this->_arrParam ['source_url'];
			
			$sys_image = $this->_arrParam ['sys_image'];
			
			$sys_game = $this->_arrParam ['sys_game'];
			
			$active = $this->_arrParam ['active'];
			
			$order = $this->_arrParam ['order'];
			
			$width = $this->_arrParam ['width'];
			
			$height = $this->_arrParam ['height'];
			
			$type_game = $this->_arrParam ['type_game'];
			
			if (! Zend_Validate::is ( $name, 'NotEmpty' ))
				$messages [] = array (
						
						'text' => "TÃªn: KhÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (! Zend_Validate::is ( $keywords, 'NotEmpty' ))
				$messages [] = array (
						
						'text' => "Tá»« khÃ³a: KhÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (! Zend_Validate::is ( $desc, 'NotEmpty' ))
				$messages [] = array (
						
						'text' => "Giá»›i thiá»‡u: KhÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (! Zend_Validate::is ( $source_fileurl, 'NotEmpty' ))
				$messages [] = array (
						
						'text' => "Nguá»“n game: KhÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (! Zend_Validate::is ( $height, 'Int' ))
				$messages [] = array (
						
						'text' => "Chiá»�u cao: Pháº£i lÃ  dáº¡ng sá»‘ vÃ  khÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (! Zend_Validate::is ( $width, 'Int' ))
				$messages [] = array (
						
						'text' => "Chiá»�u rá»™ng: Pháº£i lÃ  dáº¡ng sá»‘ vÃ  khÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (! Zend_Validate::is ( $order, 'Int' ))
				$messages [] = array (
						
						'text' => "Thá»© tá»±: Pháº£i lÃ  dáº¡ng sá»‘ vÃ  khÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (count ( $cat_game ) == 0)
				$messages [] = array (
						
						'text' => "Danh má»¥c game: KhÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (count ( $messages ) == 0) {
				
				$nameid = str_replace ( ' ', '-', $name );
				
				$nameid = strtoupper ( $nameid );
				
				$modelGame = new Admin_Model_Games ();
				
				$id = $modelGame->insertGame ( $nameid, $name, $desc, $content, $keywords, $instructions, $width, $height, "SWF", $order, $active, $source_image, $source_fileurl, $sys_image, $sys_game, $source_url, $type_game );
				
				$modelCat = new Admin_Model_CatGame ();
				
				foreach ( $cat_game as $key => $value ) {
					
					$request = $modelCat->create ( $value, $id );
				}
				
				$cache = Zend_Registry::get ( 'cache' );
				
				$cache->remove ( 'cache_new_game' );
				
				$messages [] = array (
						
						'text' => "Ä�Ã£ thÃªm game má»›i thÃ nh cÃ´ng",
						
						'type' => 'success' 
				);
			}
			
			$this->view->name = $name;
			
			$this->view->keywords = $keywords;
			
			$this->view->desc = $desc;
			
			$this->view->content = $content;
			
			$this->view->instructions = $instructions;
			
			$this->view->cat_game = $cat_game;
			
			$this->view->source_image = $source_image;
			
			$this->view->source_fileurl = $source_fileurl;
			
			$this->view->sys_image = $sys_image;
			
			$this->view->sys_game = $sys_game;
			
			$this->view->active = $active;
			
			$this->view->order = $order;
			
			$this->view->type_game = $type_game;
			
			$this->view->width = $width;
			
			$this->view->height = $height;
			
			$this->view->source_url = $source_url;
			
			$this->view->message = $messages;
		}
	}
	public function updateAction() 

	{
		$translate = Zend_Registry::get ( 'Zend_Translate' );
		
		$this->view->title = $translate->_ ( "TITLE_ADMIN_GAMES_UPDATE" );
		
		$this->view->headTitle ( $this->view->title );
		
		$menuModel = new Admin_Model_Menu ();
		
		$this->view->slbCategory = $menuModel->itemInSelectbox ();
		
		$modelGames = new Admin_Model_Games ();
		
		$modelCatGames = new Admin_Model_CatGame ();
		
		$modelServer = new Admin_Model_Server ();
		
		$listServer = $modelServer->fetchServer ( array (
				'status' => 1 
		) );
		
		$listServer = $listServer->toArray ();
		
		$url_options = array ();
		
		foreach ( $listServer as $k => $v ) {
			
			$url_options [$v ['id']] = $v ['name'] . ' - ' . $v ['url_server'];
		}
		
		$this->view->url_options = $url_options;
		
		$type_game_option = array (
				'Game Flash',
				'Video Youtube',
				'Iframe' 
		);
		
		$this->view->type_game_option = $type_game_option;
		
		$id_games = $this->_getParam ( 'id', 0 );
		
		if ($this->_request->isPost ()) {
			
			$name = $this->_arrParam ['name'];
			
			$keywords = $this->_arrParam ['keywords'];
			
			$desc = $this->_arrParam ['desc'];
			
			$content = $this->_arrParam ['content'];
			
			$instructions = $this->_arrParam ['instructions'];
			
			$cat_game = $this->_arrParam ['cat_game'];
			
			$source_image = $this->_arrParam ['source_image'];
			
			$source_fileurl = $this->_arrParam ['source_fileurl'];
			
			$sys_image = $this->_arrParam ['sys_image'];
			
			$sys_game = $this->_arrParam ['sys_game'];
			
			$active = $this->_arrParam ['active'];
			
			$flag = $this->_arrParam ['flag'];
			
			$order = $this->_arrParam ['order'];
			
			$width = $this->_arrParam ['width'];
			
			$height = $this->_arrParam ['height'];
			
			$source_url = $this->_arrParam ['source_url'];
			
			$type_game = $this->_arrParam ['type_game'];
			
			if (! Zend_Validate::is ( $name, 'NotEmpty' ))
				$messages [] = array (
						
						'text' => "TÃªn: KhÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (! Zend_Validate::is ( $keywords, 'NotEmpty' ))
				$messages [] = array (
						
						'text' => "Tá»« khÃ³a: KhÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (! Zend_Validate::is ( $desc, 'NotEmpty' ))
				$messages [] = array (
						
						'text' => "Giá»›i thiá»‡u: KhÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (! Zend_Validate::is ( $source_fileurl, 'NotEmpty' ))
				$messages [] = array (
						
						'text' => "Nguá»“n game: KhÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (! Zend_Validate::is ( $height, 'Int' ))
				$messages [] = array (
						
						'text' => "Chiá»�u cao: Pháº£i lÃ  dáº¡ng sá»‘ vÃ  khÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (! Zend_Validate::is ( $width, 'Int' ))
				$messages [] = array (
						
						'text' => "Chiá»�u rá»™ng: Pháº£i lÃ  dáº¡ng sá»‘ vÃ  khÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (! Zend_Validate::is ( $order, 'Int' ))
				$messages [] = array (
						
						'text' => "Thá»© tá»±: Pháº£i lÃ  dáº¡ng sá»‘ vÃ  khÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (count ( $cat_game ) == 0)
				$messages [] = array (
						
						'text' => "Danh má»¥c game: KhÃ´ng Ä‘Æ°á»£c Ä‘á»ƒ trá»‘ng",
						
						'type' => 'errormsg' 
				)
				;
			
			if (count ( $messages ) == 0) {
				
				$nameid = str_replace ( ' ', '-', $name );
				
				$nameid = strtoupper ( $nameid );
				
				$id = $modelGames->updateGame ( $id_games, $nameid, $name, $desc, $content, $keywords, $instructions, $width, $height, "SWF", $order, $active, $source_image, $source_fileurl, $sys_image, $sys_game, $source_url, $type_game, $flag );
				
				$deleteCat = $modelCatGames->delete ( 'game_id=' . $id_games );
				
				foreach ( $cat_game as $key => $value ) {
					
					$request = $modelCatGames->create ( $value, $id_games );
				}
				
				$messages [] = array (
						
						'text' => "Ä�Ã£ update thÃ nh cÃ´ng",
						
						'type' => 'success' 
				);
			}
			
			$this->view->name = $name;
			
			$this->view->keywords = $keywords;
			
			$this->view->desc = $desc;
			
			$this->view->content = $content;
			
			$this->view->instructions = $instructions;
			
			$this->view->cat_game = $cat_game;
			
			$this->view->source_image = $source_image;
			
			$this->view->source_fileurl = $source_fileurl;
			
			$this->view->sys_image = $sys_image;
			
			$this->view->sys_game = $sys_game;
			
			$this->view->active = $active;
			
			$this->view->order = $order;
			
			$this->view->width = $width;
			
			$this->view->height = $height;
			
			$this->view->flag = $flag;
			
			$this->view->source_url = $source_url;
			
			$this->view->message = $messages;
		} else {
			
			$data = $modelGames->fetchRow ( 'id=' . $id_games );
			
			$data = $data->toArray ();
			
			$data_cat = $modelCatGames->fetchAll ( 'game_id=' . $id_games );
			
			$data_cat = $data_cat->toArray ();
			
			$cat_game = array ();
			
			foreach ( $data_cat as $key => $value ) {
				
				$cat_game [] = $value ['cat_id'];
			}
			
			$this->view->name = $data ['name'];
			
			$this->view->keywords = $data ['keywords'];
			
			$this->view->desc = $data ['desc'];
			
			$this->view->content = $data ['content'];
			
			$this->view->instructions = $data ['instructions'];
			
			$this->view->cat_game = $cat_game;
			
			$this->view->source_image = $data ['source_image'];
			
			$this->view->source_fileurl = $data ['source_fileurl'];
			
			$this->view->sys_image = $data ['sys_image'];
			
			$this->view->sys_game = $data ['sys_game'];
			
			$this->view->active = $data ['active'];
			
			$this->view->order = $data ['order'];
			
			$this->view->width = $data ['width'];
			
			$this->view->height = $data ['height'];
			
			$this->view->flag = $data ['flag'];
			
			$this->view->source_url = $data ['source_url'];
		}
	}
	public function activeAction() {
		$this->_helper->layout ()->disableLayout ();
		
		$type = $this->_request->getParam ( 'type', 1 );
		
		$id = $this->_request->getParam ( 'id', 0 );
		
		if ($type > 0) {
			
			$type = 1;
		} else {
			
			$type = 0;
		}
		
		$modelGames = new Admin_Model_Games ();
		
		$modelGames->update ( array (
				'active' => $type 
		), 'id=' . $id );
		
		$this->_redirect ( $this->_actionMain );
	}
	public function hotAction() {
		$this->_helper->layout ()->disableLayout ();
		
		$id = $this->_request->getParam ( 'id', 0 );
		
		$modelGames = new Admin_Model_Games ();
		
		$modelGames->update ( array (
				'game_hot' => 1 
		), 'id=' . $id );
		
		$this->_redirect ( $this->_actionMain );
	}
	public function updateorderAction() 

	{
		$this->_helper->layout ()->disableLayout ();
		
		$gameModel = new Admin_Model_Games ();
		
		if (count ( $_GET )) {
			
			$item = $this->_arrParam ['item'];
			
			if (count ( $item )) {
				
				foreach ( $item as $k => $v ) {
					
					if ($v == '' || $v < 0) {
						
						$v = 0;
					}
					
					if ($v > 999999) {
						
						$v = 999999;
					}
					
					$gameModel->updateOrder ( $k, $v );
				}
			}
		}
		
		$this->_redirect ( $this->_actionMain );
	}
	public function deleteAction() 

	{
		$translate = Zend_Registry::get ( 'Zend_Translate' );
		
		$this->view->title = $translate->_ ( "TITLE_ADMIN_GAMES_DELETE" );
		
		$this->view->headTitle ( $this->view->title );
		
		if ($this->getRequest ()->isPost ()) {
			
			$del = $this->getRequest ()->getPost ( 'del' );
			
			if ($del == 'Yes') {
				
				$id = $this->getRequest ()->getPost ( 'id' );
				
				$gameModel = new Admin_Model_Games ();
				
				$result = $gameModel->delete ( 'id=' . $id );
			}
			
			$this->_redirect ( $this->_actionMain );
		} else {
			
			$id = $this->_getParam ( 'id', 0 );
			
			$this->view->gid = $id;
		}
	}
	public function syncAction() 

	{
		$this->_helper->layout ()->disableLayout ();
		
		/*
		 * $menuModel = new Admin_Model_Menu(); $listMenu = $menuModel->fetchMenu(); $listMenu = $listMenu->toArray(); $gameModel = new Admin_Model_Games(); $listGame = $gameModel->fetchGames(); $listGame = $listGame->toArray(); $catgameModel = new Admin_Model_CatGame(); //print_r($listGame); $i = 0; foreach ($listMenu as $km=>$vm){ if($i>=0 && $i<=5){ foreach ($listGame as $kg =>$vg){ $pos = stristr($vg['cat'], $vm['name']); if($pos === false){ }else{ $catgameModel->create($vm['id'],$vg['id']); } } } if($i>5){ break; } $i++; }
		 */
		
		echo 'finish';
	}
	public function deleteitemsAction() {
		$this->_helper->layout ()->disableLayout ();
		
		$gameModel = new Admin_Model_Games ();
		
		$item = $this->_arrParam ['cid'];
		
		foreach ( $item as $k => $v ) {
			
			$gameModel->delete ( 'id=' . $v );
		}
		
		$this->_redirect ( $this->_actionMain );
	}
}